package com.nuorui.module.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuorui.common.core.code.UserErrorCode;
import com.nuorui.common.core.domain.ResponseDTO;
import com.nuorui.common.core.enums.ConfigKeyEnum;
import com.nuorui.common.utils.SmartBeanUtil;
import com.nuorui.module.system.mapper.ConfigMapper;
import com.nuorui.module.system.domain.entity.ConfigEntity;
import com.nuorui.module.system.domain.form.ConfigForm;
import com.nuorui.module.system.domain.query.ConfigQueryForm;
import com.nuorui.module.system.domain.vo.ConfigVO;
import com.nuorui.module.system.manager.ConfigManager;
import com.nuorui.module.system.service.ConfigService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 配置信息
 *
 * @author: Fred
 * @email 453086@qq.com
 * @create: 2025-06-12 09:16
 */
@Slf4j
@Service
@AllArgsConstructor
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, ConfigEntity> implements ConfigService {

    private final ConfigManager configManager;


    /**
     * 分页
     *
     * @param page
     * @param queryForm
     * @return
     */
    @Override
    public ResponseDTO getConfigsPage(Page page, ConfigQueryForm queryForm) {
        return ResponseDTO.ok(page.setRecords(baseMapper.selectConfigPage(page, queryForm)));
    }

    /**
     * 添加系统配置
     */
    @Override
    public ResponseDTO saveConfig(ConfigForm configForm) {

        ConfigEntity entity = baseMapper.selectByKey(configForm.getConfigKey());
        if (null != entity) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST);
        }
        entity = SmartBeanUtil.copy(configForm, ConfigEntity.class);
        baseMapper.insert(entity);

        configManager.refreshConfigCache(entity.getConfigId());
        return ResponseDTO.ok();
    }

    @Override
    public ResponseDTO<String> updateConfig(Long configId, ConfigForm configForm) {

        ConfigEntity existing = baseMapper.selectById(configId);
        if (null == existing) {
            return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
        }
        // 检查 key 冲突
        ConfigEntity sameKeyEntity = baseMapper.selectByKey(configForm.getConfigKey());
        if (sameKeyEntity != null && !Objects.equals(sameKeyEntity.getConfigId(), configId)) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST, "configKey 已存在");
        }
        // 更新数据
        existing = SmartBeanUtil.copy(configForm, ConfigEntity.class);
        baseMapper.updateById(existing);

        // 刷新缓存
        configManager.refreshConfigCache(configId);
        return ResponseDTO.ok();
    }

    @Override
    public ResponseDTO updateValueByKey(ConfigKeyEnum key, String value) {
        ConfigVO config = configManager.getConfig(key);
        if (null == config) {
            return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
        }

        ConfigForm configForm = SmartBeanUtil.copy(config, ConfigForm.class);
        configForm.setConfigValue(value);
        return updateConfig(config.getConfigId(), configForm);
    }

    @Override
    public ResponseDTO removeConfig(Long configId) {

        ConfigEntity existing = baseMapper.selectById(configId);
        if (existing == null) {
            return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
        }

        // 删除数据库记录
        baseMapper.deleteById(configId);

        // 从缓存中移除
        configManager.removeConfigCache(existing.getConfigKey());
        return ResponseDTO.ok();
    }

    @Override
    public String getConfigValue(ConfigKeyEnum configKey) {
        ConfigVO config = configManager.getConfig(configKey);
        return config == null ? null : config.getConfigValue();
    }
}
